//
//  RewardTableViewCell.m
//  jingjing2.0
//
//  Created by apple on 16/5/24.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "RewardTableViewCell.h"
#import "WebViewController.h"
#import "GetEggRewardViewController.h"
#import "RealRewardViewController.h"
#import "NetRewardViewController.h"
@implementation RewardTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.getReward.layer.cornerRadius=4;
    self.getReward.clipsToBounds=YES;
    self.getReward.layer.borderColor=BLUECOLOR.CGColor;
    self.getReward.layer.borderWidth=1.0;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}
//领取奖励
- (IBAction)goToGetReward:(id)sender {
    CustomButton *btn=sender;
    //判断奖励类型   1净净优惠券(要领取)，2实物，3流量包,4带链接第三方优惠券，5带优惠券码第三方优惠券（自动领取）
    switch (btn.number) {
        case 1:
            [self getCouponWithNoteID:btn.tag];
            break;
        case 2:
            //跳界面
            [self goToRealRewardViewControllerWithNoteID:btn.tag];
            break;
        case 3:
            //跳界面
            [self goToNetViewControllerWithNoteID:btn.tag andTitle:btn.title];
            break;
        case 4:
        {
            //跳网页
            WebViewController *web=[[WebViewController alloc]init];
            web.pageUrl=btn.pageUrl;
            web.title=@"领取奖励";
            web.hidesBottomBarWhenPushed=YES;
            GetEggRewardViewController *egg=(GetEggRewardViewController *)_delegate;
            [egg.navigationController pushViewController:web animated:YES];
        }
            break;
        case 5:
            break;
        default:
            break;
    }

 
}
-(void)goToNetViewControllerWithNoteID:(NSInteger)noteID andTitle:(NSString *)title
{
    NetRewardViewController *netReward=[[NetRewardViewController alloc]init];
    GetEggRewardViewController *egg=(GetEggRewardViewController *)_delegate;
    netReward.hidesBottomBarWhenPushed=YES;
    netReward.noteId=noteID;
    netReward.prizeTitle=title;
    [egg.navigationController pushViewController:netReward animated:YES];
}
-(void)goToRealRewardViewControllerWithNoteID:(NSInteger)noteID
{
    RealRewardViewController *realReward=[[RealRewardViewController alloc]init];
    GetEggRewardViewController *egg=(GetEggRewardViewController *)_delegate;
    realReward.hidesBottomBarWhenPushed=YES;
    realReward.noteID=noteID;
    [egg.navigationController pushViewController:realReward animated:YES];
}
-(void)getCouponWithNoteID:(NSInteger)NoteID
{
    //http://192.168.1.107:3722/Activity/ColorEggsSubmitCoupon
    // m_ColorEggsSubmitCouponPara(UserCode,GetNoteID,Timestamp,Sign)
    NSString *noteId=[NSString stringWithFormat:@"%ld",NoteID];
    NSString *timeStamp=[GeneralTool gettimestamp];
    NSDictionary *dic=@{@"UserCode":GETUSERCODE,
                        @"GetNoteID":noteId,
                        @"Timestamp":timeStamp};
    NSString *sign=[GeneralTool GetSin:dic];
    NSDictionary *dataDic=@{@"UserCode":GETUSERCODE,
                            @"GetNoteID":noteId,
                            @"Timestamp":timeStamp,
                            @"Sign":sign};
    NSDictionary *requestDic=@{@"m_ColorEggsSubmitCouponPara":dataDic};
    [RequestTool requestDataWithType:@"post" andUrlString:@"Activity/ColorEggsSubmitCoupon" andParameters:requestDic andRequestFinished:^(id responses, NSError *error) {
        if (error) {
            [MBProgressHUD showError:@"未知错误，请稍后再试"];
        }
        if ([responses[@"IsSuccess"]integerValue]==1) {
            if ([[responses[@"ReturnObject"]stringByRemovingPercentEncoding] isEqualToString:@"提交成功"])
            {
                
                self.getReward.layer.borderColor=GRAYCOLOR.CGColor;
                [self.getReward setTitle:@"已领取" forState:UIControlStateNormal];
                [self.getReward setTitleColor:GRAYCOLOR forState:UIControlStateNormal];
                self.getReward.enabled=NO;
            }
        }else
        {
           [MBProgressHUD showError:responses[@"ReturnObject"]];
        }
    }];
}

@end
